
package org.sidora.core.util;

import java.util.Iterator;
import java.util.List;

import com.db4o.ObjectContainer;

import org.sidora.core.KeyObject;
import org.sidora.core.context.Context;
import org.sidora.core.context.Equality;
import org.sidora.core.context.StratigraphicRelation;

/**
 * AbstractInventory
 * @author Enric Tartera, Juan Manuel Gimeno, Roger Masgoret
 * @version 1.0
 */
public abstract class AbstractInventory {
    
    public abstract Object getNewObject(ParameterCollector param) throws Exception;
    protected abstract KeyObject getNewKey(ParameterCollector param);
    public abstract Object getObject(ParameterCollector param);
    //public abstract List getList();
    
    
    public static void setObject(Object obj) {
        
        try{
            ObjectContainer db = OCProvider.getInstance();
            db.set(obj);
            db.commit();
        }catch (Exception e){
            System.out.println(e.toString());
        }
        
    }
    
    
    public static void deleteObject(Object obj) {
        if (obj instanceof Context){
            //Borrem les relacions entre Context
            List<StratigraphicRelation> lt = StratigraphicRelation.Inventory.getList();
            Iterator<StratigraphicRelation> it = lt.iterator();
            StratigraphicRelation sr;
            while (it.hasNext()){
                sr = (StratigraphicRelation)it.next();
                if ((sr).hasContext((Context)obj))
                    AbstractInventory.deleteObject(sr);
            }
        }
        
        ObjectContainer db = OCProvider.getInstance();
        db.delete(obj);
        db.commit();
        
    }
    
}
